// Fill out your copyright notice in the Description page of Project Settings.


#include "Actor/TargetsManager.h"
#include "Components/BillboardComponent.h"
#include "Libraries/MissionSystemFL.h"
#include "Component/MissionComponent.h"

ATargetsManager::ATargetsManager()
{
 	PrimaryActorTick.bCanEverTick = false;
	Visualizer = CreateDefaultSubobject<UBillboardComponent>(FName("Visualizer"));
}

void ATargetsManager::BeginPlay()
{
	Super::BeginPlay();
	
}

void ATargetsManager::Assign_Target(FTargetInfo TargetInfo)
{
	if (CanAddToTargets(TargetInfo.TargetUniqueName)) {
		Targets.Add(TargetInfo);
		if (UMissionSystemFL::GetMissionComponent()->IsValidLowLevel()) {
			UMissionSystemFL::GetMissionComponent()->UpdateMarkerTargetInfo();
		}
	}
}

void ATargetsManager::Remove_Target(FTargetInfo TargetInfo)
{
	int32 RemovedIndex = FindTargetIndex(TargetInfo);
	if(Targets.IsValidIndex(RemovedIndex)) Targets.RemoveAt(RemovedIndex);
	if (UMissionSystemFL::GetMissionComponent()->IsValidLowLevel()) {
		UMissionSystemFL::GetMissionComponent()->UpdateMarkerTargetInfo();
	}
}

bool ATargetsManager::CanAddToTargets(FName TargetInfo)
{
	if (Targets.Num() != 0) {
		for (const auto& Elem : Targets) {
			if (Elem.TargetUniqueName == TargetInfo) {
				return true;
			}
		}

	}
	return false;
}

int32 ATargetsManager::FindTargetIndex(FTargetInfo TargetInfo)
{
	if (Targets.Num() != 0) {
		int i = 0;
		for (const auto& Elem : Targets) {
			if (Elem.TargetUniqueName == TargetInfo.TargetUniqueName) {
				return i;
			}
			++i;
		}
	}
	return -1;
}

